//
//  CheckVerifiCodeViewController.m
//  HeartWorldClient
//
//  Created by Mac on 2017/7/13.
//  Copyright © 2017年 谭明权. All rights reserved.
//

#import "CheckVerifiCodeViewController.h"
#import "YCInputVerifyCodeView.h"
#import "YCLoginManager.h"
#import <CoreLocation/CoreLocation.h>
#import "YCAlertView.h"
#import "YCResetPasswordViewController.h"
#import "AccountPasswordLoginViewController.h"
#import "YCTabbarViewController.h"

#define kCountTimeNum 60

@interface CheckVerifiCodeViewController ()//<CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UIView *containView;
@property (weak, nonatomic) IBOutlet UILabel *phoneNumLabel;
@property (weak, nonatomic) IBOutlet UIButton *timeDesButton;

@property (weak, nonatomic) IBOutlet UIButton *voiceDesButton;

@property (weak, nonatomic) IBOutlet UIButton *pwdLoginButton;
@property (nonatomic, strong) CLLocationManager *mgr;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger countDownNumber;
@property (nonatomic, assign) NSInteger codeCanNotReceNum;
@property(nonatomic,weak)YCInputVerifyCodeView *inputView;


@end

@implementation CheckVerifiCodeViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    //产品说隐藏
    _pwdLoginButton.hidden = YES;
    
    [self setBackItem];
    
    self.phoneNumLabel.text = [NSString stringWithFormat:@"验证码已发送至%@",self.phoneStr];
    
    if (self.isGetCodeAgain&&self.typeFrom == TypeFromRegister) {
        
        [_timeDesButton setTitle:@"60s后重新获取" forState:UIControlStateNormal];
        
        [self getVerfiCodeBtnClick:nil];
    }
 
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    _codeCanNotReceNum = 0;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];

    [self initializeConfig];
    
}
- (void)initializeConfig {
    
    switch (self.typeFrom) {
        case TypeFromLogin:
            self.title = @"登录HMO管家";
            self.pwdLoginButton.hidden = NO;
            [self getVerfiCodeBtnClick:nil];
            break;
        case TypeFromRegister:
            self.title = @"注册";
            self.pwdLoginButton.hidden = YES;
            break;
        case TypeFromGetBackPwd:
            self.title = @"找回登录密码";
            self.pwdLoginButton.hidden = YES;
            [self getVerfiCodeBtnClick:nil];
            break;
        default:
            break;
    }
    
    YCInputVerifyCodeView *inputView = [YCInputVerifyCodeView loadFromNib:@"YCInputVerifyCodeView"];
    
    self.inputView = inputView;
    
    weakify(self)
    
    [inputView inputFinish:^(NSString *code) {
        strongify(self);
        //验证是否正确真确的话进行登录
        [self verifyCodeLoginWithCode:code];
    }];
    
    [self.containView addSubview:inputView];
    
    [inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.containView);
    }];
    
}

//使用验证码登录
- (void)verifyCodeLoginWithCode:(NSString *)code {
    
    //验证验证码是否真确
    if (self.typeFrom==TypeFromGetBackPwd) {
        
        [[YCLoginManager shareManager]verifyCodeVerifyWithCode:code complete:^(BOOL success, NSString *message) {
            if (success) {
                
                YCResetPasswordViewController *resetPwd = [[YCResetPasswordViewController alloc]init];
                resetPwd.phoneNum = [self.phoneStr cleanTelephoneNumber];
                [[UIApplication sharedApplication].keyWindow presentMessageTips:@"验证成功"];
                [self showViewController:resetPwd sender:nil];
                
            }else{
                [[UIApplication sharedApplication].keyWindow presentMessageTips:@"验证失败"];
            }
        }];
        return;
    }
    NSLog(@"登录验证！！！code:%@", code);
    [self presentLoadingTips:nil];
    [[YCLoginManager shareManager] verifyCodeLoginWithCode:code success:^(NSString *memberId) {
        [self dismissTips];
        [[YCLoginManager shareManager] loginSuccessConfigMemberId:memberId];
        
//        [self.navigationController popToRootViewControllerAnimated:YES];
        
        self.view.window.rootViewController = [YCTabbarViewController new];
        
    } failure:^(NSString *message) {
        [self presentFailureTips:message];
        [self.inputView inputCodeAgain];
    }];
}

- (void)beginTimer {
    _countDownNumber = kCountTimeNum;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countdown) userInfo:nil repeats:YES];
     [_timeDesButton setTitle: [NSString stringWithFormat:@"%@s后重新获取",@(_countDownNumber).stringValue] forState:UIControlStateDisabled];
}

- (void)countdown {
    
    _countDownNumber --;
    if (self.countDownBlock) {
        self.countDownBlock(_countDownNumber);
    }
    [_timeDesButton setTitle: [NSString stringWithFormat:@"%@s后重新获取",@(_countDownNumber).stringValue] forState:UIControlStateDisabled];
    if (_countDownNumber <= 0) {
        [_timer invalidate];
        _timer = nil;
        [_timeDesButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_timeDesButton setTitleColor:[UIColor colorWithHexString:@"187fc4"] forState:UIControlStateNormal];
        [_voiceDesButton setTitleColor:[UIColor colorWithHexString:@"187fc4"] forState:UIControlStateNormal];
        self.timeDesButton.enabled = YES;
    }else{
        [_timeDesButton setTitleColor:[UIColor colorWithHexString:@"999999"] forState:UIControlStateDisabled];
        [_voiceDesButton setTitleColor:[UIColor colorWithHexString:@"999999"] forState:UIControlStateNormal];
    }
}

- (IBAction)getVoiceBtnClick:(id)sender {
    
    if ( self.timeDesButton.isEnabled==NO) {
        [self presentMessageTips:@"验证码获取中，请稍后"];
        return;
    }
    
    weakify(self)
    
     [self presentLoadingTips:nil];
    
    [_timeDesButton setTitleColor:[UIColor colorWithHexString:@"999999"] forState:UIControlStateDisabled];
    [_voiceDesButton setTitleColor:[UIColor colorWithHexString:@"999999"] forState:UIControlStateNormal];
    
    [[YCLoginManager shareManager]getVoiceVerifyCode:^(BOOL success, NSString *message) {
        
         strongify(self);
        
        if (success) {
            self.timeDesButton.enabled = NO;
            [self beginTimer];
            [self dismissTips];
        }else{
            
            [self presentFailureTips:message];
        }
        
    }];
}


//点击重新获取验证码
- (IBAction)getVerfiCodeBtnClick:(id)sender {
    
    //已经点击了三次但是没有收到验证码,这时点击获取语音验证码
    if (_codeCanNotReceNum>=3) {
        
        [[YCAlertView sharedAlertView]showAlertView];
        
    }else{
        
        _codeCanNotReceNum ++;
        
        weakify(self)
        
        [self presentLoadingTips:nil];
        
        [_timeDesButton setTitleColor:[UIColor colorWithHexString:@"999999"] forState:UIControlStateDisabled];
        [_voiceDesButton setTitleColor:[UIColor colorWithHexString:@"999999"] forState:UIControlStateNormal];
        
        [[YCLoginManager shareManager]getVerifyCode:^(BOOL success, NSString *message) {
            
            strongify(self);
            
            if (success == YES) {
                self.timeDesButton.enabled = NO;
                [self beginTimer];
                [self dismissTips];
                
            }else{
                
                [self presentFailureTips:message];
            }
            
        }];
    }
  
}
- (IBAction)usePasswordLogin:(id)sender {

    AccountPasswordLoginViewController *vc = [[AccountPasswordLoginViewController alloc] init];
    [self showViewController:vc sender:nil];
}


-(void)dealloc{
    [_timer invalidate];
    _timer = nil;
    self.mgr = nil;
    self.mgr.delegate = nil;
    
    [self clearKeyboardCorverNotificationAndGesture];
    [self tapGestureHandel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
